package team.stride.gmap;

import java.util.ArrayList;

import team.stride.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class PathDraw extends Overlay {

	private final ArrayList<GePoint> pathPoints;

	private Point point;
	private Point point2;
	private Paint paint;
	private RectF rect;
	private Projection p;
	private static final int START_RADIUS = 10;
	private static final int PATH_WIDTH = 6;
	private Context context;

	public PathDraw(ArrayList<GePoint> epathPoints, Context c) {
		super();
		context = c;
		pathPoints = epathPoints;
		point = new Point();
		point2 = new Point();
		paint = new Paint();
		rect = new RectF();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
		paint.setARGB(255, 255, 69, 0);
		p = mapView.getProjection();
		// if(pathPoints.size()>0)
		GeoPoint g1, g2;
		g1 = new GeoPoint(pathPoints.get(0).getLat(), pathPoints.get(0)
				.getLong());
		p.toPixels(g1, point);
		rect.set(point.x - START_RADIUS, point.y - START_RADIUS, point.x
				+ START_RADIUS, point.y + START_RADIUS);
		// canvas.drawOval(rect, paint);
		if (pathPoints.size() > 1) {
			g2 = new GeoPoint(pathPoints.get(1).getLat(), pathPoints.get(1)
					.getLong());
			p.toPixels(g2, point2);
			paint.setARGB(150, 255, 69, 0);
			paint.setStrokeWidth(PATH_WIDTH);
			canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
		}
		int i = 1;
		for (; i < pathPoints.size() - 1; i++) {
			GeoPoint g11 = new GeoPoint(pathPoints.get(i).getLat(), pathPoints
					.get(i).getLong());
			GeoPoint g22 = new GeoPoint(pathPoints.get(i + 1).getLat(),
					pathPoints.get(i + 1).getLong());
			p.toPixels(g11, point);
			p.toPixels(g22, point2);
			canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
		}
		// 255;127;0
		paint.setARGB(255, 255, 127, 0);
		g1 = new GeoPoint(pathPoints.get(0).getLat(), pathPoints.get(0)
				.getLong());
		p.toPixels(g1, point);
		// rect.set(point.x - START_RADIUS, point.y - START_RADIUS, point.x
		// + START_RADIUS, point.y + START_RADIUS);
		Bitmap bmp2 = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.psuhpinstr);
		canvas.drawBitmap(bmp2, point.x, point.y - 42, null);
		if (pathPoints.size() > 1) {
			paint.setARGB(255, 255, 0, 0);
			g1 = new GeoPoint(pathPoints.get(pathPoints.size() - 1).getLat(),
					pathPoints.get(pathPoints.size() - 1).getLong());
			p.toPixels(g1, point);
			Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.pushpin);
			canvas.drawBitmap(bmp, point.x - 52, point.y - 62, null);
			/*
			 * p.toPixels(g1, point); rect.set(point.x - START_RADIUS, point.y -
			 * START_RADIUS, point.x + START_RADIUS, point.y + START_RADIUS);
			 * canvas.drawOval(rect, paint);
			 */
		}
	}
}
